﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    /// <summary>
    /// 公用工具箱
    /// </summary>
    public static class ToolBox
    {
        /// <summary>
        /// 得到月份的最后一天
        /// </summary>
        /// <param name="year">年份</param>
        /// <param name="month">月份</param>
        /// <returns></returns>
        public static int GetEndDay(int year,int month)
        {
            int result = 31;
            switch (month)
            {
                case 1:
                    break;
                case 2:
                    if (DateTime.IsLeapYear(year))
                        result = 29;
                    else
                        result = 28;
                    break;
                case 3:
                    break;
                case 4:
                    result = 30;
                    break;
                case 5:
                    break;
                case 6:
                    result = 30;
                    break;
                case 7:
                    break;
                case 8:
                    break;
                case 9:
                    result = 30;
                    break;
                case 10:
                    break;
                case 11:
                    result = 30;
                    break;
                case 12:
                    result = 30;
                    break;
                default :
                    throw new ArgumentException("参数异常");
            }

            return result;
        }

        /// <summary>
        /// 传入时间，求出此时间的上一季度
        /// </summary>
        /// <param name="_Date"></param>
        /// <returns></returns>
        public static QuarterOfYear GetPreviousQuarter(DateTime _Date)
        {
            QuarterOfYear result = new QuarterOfYear();
            result.Year = _Date.Year;

            switch (_Date.Month)
            { 
                case 1:
                    result.Year -= 1;
                    result.Num = 4;
                    result.BeginDate = new DateTime(result.Year, 10, 1);
                    result.EndDate = new DateTime(result.Year, 12, 31);
                    break;
                case 2:
                    result.Year -= 1;
                    result.Num = 4;
                    result.BeginDate = new DateTime(result.Year, 10, 1);
                    result.EndDate = new DateTime(result.Year, 12, 31);
                    break;
                case 3:
                    result.Year -= 1;
                    result.Num = 4;
                    result.BeginDate = new DateTime(result.Year, 10, 1);
                    result.EndDate = new DateTime(result.Year, 12, 31);
                    break;
                case 4:
                    result.Num = 1;
                    result.BeginDate = new DateTime(result.Year, 1, 1);
                    result.EndDate = new DateTime(result.Year, 3, 31);
                    break;
                case 5:
                    result.Num = 1;
                    result.BeginDate = new DateTime(result.Year, 1, 1);
                    result.EndDate = new DateTime(result.Year, 3, 31);
                    break;
                case 6:
                    result.Num = 1;
                    result.BeginDate = new DateTime(result.Year, 1, 1);
                    result.EndDate = new DateTime(result.Year, 3, 31);
                    break;
                case 7:
                    result.Num = 2;
                    result.BeginDate = new DateTime(result.Year, 4, 1);
                    result.EndDate = new DateTime(result.Year, 6, 30);
                    break;
                case 8:
                    result.Num = 2;
                    result.BeginDate = new DateTime(result.Year, 4, 1);
                    result.EndDate = new DateTime(result.Year, 6, 30);
                    break;
                case 9:
                    result.Num = 2;
                    result.BeginDate = new DateTime(result.Year, 4, 1);
                    result.EndDate = new DateTime(result.Year, 6, 30);
                    break;
                case 10:
                    result.Num = 3;
                    result.BeginDate = new DateTime(result.Year, 7, 1);
                    result.EndDate = new DateTime(result.Year, 9, 30);
                    break;
                case 11:
                    result.Num = 3;
                    result.BeginDate = new DateTime(result.Year, 7, 1);
                    result.EndDate = new DateTime(result.Year, 9, 30);
                    break;
                case 12:
                    result.Num = 3;
                    result.BeginDate = new DateTime(result.Year, 7, 1);
                    result.EndDate = new DateTime(result.Year, 9, 30);
                    break;
            }
            return result;
        }

        /// <summary>
        /// 传入时间，求出此时间的上半年度
        /// </summary>
        /// <param name="_Date"></param>
        /// <returns></returns>
        public static HalfYearObject GetPreviousHalfYear(DateTime _Date)
        {
            HalfYearObject result = new HalfYearObject();
            if (_Date.Month >= 1 && _Date.Month <= 6)
            {
                result.Year = _Date.Year - 1;
                result.BeginDate = new DateTime(result.Year, 6, 1);
                result.EndDate = new DateTime(result.Year, 12, 31);
            }
            else
            {
                result.Year = _Date.Year;
                result.BeginDate = new DateTime(result.Year, 1, 1);
                result.EndDate = new DateTime(result.Year, 6, 30);
            }
            return result;
        }
    }
}
